using ETWJ.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace ETWJ.Service
{
    public class MyDbContext : DbContext
    {
        public DbSet<Company> Companys { get; set; }
        public DbSet<Employee> Employees { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderTrack> OrderTracks { get; set; }
        public DbSet<Permission> Permissions { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<ProductOrder> ProductOrders { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<RolePermission> RolePermissions { get; set; }
        public DbSet<Setting> Settings { get; set; }
        public DbSet<UserRole> UserRoles { get; set; }
        public DbSet<Cart> Carts { get; set; }

        public MyDbContext()
        {
            this.Database.EnsureCreated();
        }


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseMySql(@"Server=127.0.0.1;database=etwj;uid=root;pwd=root;");//Convert Zero Datetime=True;Allow Zero Datetime=True
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            var company = modelBuilder.Entity<Company>();
            company.ToTable("T_Companys");

            var employee = modelBuilder.Entity<Employee>();
            employee.ToTable("T_Employees");

            var order = modelBuilder.Entity<Order>();
            order.ToTable("T_Orders");

            var cart = modelBuilder.Entity<Cart>();
            cart.ToTable("T_Carts");


            var orderTrack = modelBuilder.Entity<OrderTrack>();
            orderTrack.ToTable("T_OrderTracks");
            orderTrack.HasOne(e => e.Order).WithMany().HasForeignKey(e => e.OrderId).IsRequired();
            orderTrack.HasOne(e => e.Product).WithMany().HasForeignKey(e => e.ProductId).IsRequired();



            var permission = modelBuilder.Entity<Permission>();
            permission.ToTable("T_Permissions");

            var product = modelBuilder.Entity<Product>();
            product.ToTable("T_Products");
            product.Property(e => e.Price).HasMaxLength(2);


            var productOrder = modelBuilder.Entity<ProductOrder>();
            productOrder.ToTable("T_ProductOrder");
            productOrder.HasOne(e => e.Product).WithMany().HasForeignKey(e => e.ProductId).IsRequired();
            productOrder.HasOne(e => e.Order).WithMany().HasForeignKey(e => e.OrderId).IsRequired();


            var role = modelBuilder.Entity<Role>();
            role.ToTable("T_Roles");

            var rolePermission = modelBuilder.Entity<RolePermission>();
            rolePermission.ToTable("T_RolePermissions");
            rolePermission.HasOne(e => e.Role).WithMany().HasForeignKey(e => e.RoleId).IsRequired();
            rolePermission.HasOne(e => e.Permission).WithMany().HasForeignKey(e => e.PermissionId).IsRequired();


            var setting = modelBuilder.Entity<Setting>();
            setting.ToTable("T_Settings");

            var userRole = modelBuilder.Entity<UserRole>();
            userRole.ToTable("T_UserRoles");
            userRole.HasOne(e => e.Employee).WithMany().HasForeignKey(e => e.UserId).IsRequired();
            userRole.HasOne(e => e.Role).WithMany().HasForeignKey(e => e.RoleId).IsRequired();


        }
    }
}
